package com.exception;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class HandlerException implements HandlerExceptionResolver{

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
        LoginException loginException = null;
        String message = null;
        if(ex instanceof LoginException){
        	loginException = (LoginException)ex;
        }else{
        	loginException = new LoginException("未知错误");
        }
        message = loginException.getMessage();
        request.setAttribute("message", message);
        try {
			request.getRequestDispatcher("/WEB-INF/views/error/401.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
		return new ModelAndView();
	}

}
